package ua.com.fesvlast.hotel.model.commands;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ua.com.fesvlast.hotel.controller.managers.ConfigurationManager;
import ua.com.fesvlast.hotel.controller.managers.MessageManager;
import ua.com.fesvlast.hotel.model.dao.DAOFactory;
import ua.com.fesvlast.hotel.model.enteties.Contacts;
import ua.com.fesvlast.hotel.model.enteties.Users;
import ua.com.fesvlast.hotel.model.utils.Validator;

public class RegistrationCommand implements ICommand{
	
	private static String PARAM_NAME_FIRST_NAME="firstName";
	private static String PARAM_NAME_LAST_NAME="lastName";
	private static String PARAM_PHONE="phone";
	private static String PARAM_NAME_EMAIL="email";
	private static String PARAM_NAME_ADDRESS="address";
	private static String PARAM_NAME_CITY="city";
	private static String PARAM_NAME_COUNTRY="country";
	
	private static String PARAM_LOGIN="login";
	private static String PARAM_NAME_FIRST_PASSWORD="firstPassword";
	private static String PARAM_NAME_SECOND_PASSWORD="secondPassword";
	
	
	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String firstName=request.getParameter(PARAM_NAME_FIRST_NAME);
		String lastName=request.getParameter(PARAM_NAME_LAST_NAME);
		String phone=request.getParameter(PARAM_PHONE);
		String email=request.getParameter(PARAM_NAME_EMAIL);
		String address=request.getParameter(PARAM_NAME_ADDRESS);
		String city=request.getParameter(PARAM_NAME_CITY);
		String country =request.getParameter(PARAM_NAME_COUNTRY);
		
		String passwordFirst=request.getParameter(PARAM_NAME_FIRST_PASSWORD);
		String passwordSecond=request.getParameter(PARAM_NAME_SECOND_PASSWORD);
		String login =request.getParameter(PARAM_LOGIN);
		
		String page=null;
		
		Users user =new Users();
		user.setLogin(login);
		user.setPassword(passwordFirst);
		user.setUserType("user");
		
		if(!Validator.validateUser(user)){
			request.setAttribute("errorMessage",Validator.getMessage() );
			return page=ConfigurationManager.getInstance().getProperty(
					ConfigurationManager.REGISTRATION_PAGE);
		}
		
		if(!passwordFirst.equals(passwordSecond)){
			request.setAttribute("errorMessage","You entered the different passwords." );
			
			return page=ConfigurationManager.getInstance().getProperty(
					ConfigurationManager.REGISTRATION_PAGE);
		}
		
		user=DAOFactory.getUsersDAO().createUser(user);
		
		
		Contacts contact =new Contacts();
		contact.setAddress(address);
		contact.setCity(city);
		contact.setCountry(country);
		contact.setFirstName(firstName);
		contact.setLastName(lastName);
		contact.setEmail(email);
		contact.setPhone(phone);
		contact.setUserId(user.getUserId());
		
		if(!Validator.validateContact(contact)){
			request.setAttribute("errorMessage",Validator.getMessage() );
			return page=ConfigurationManager.getInstance().getProperty(ConfigurationManager.REGISTRATION_PAGE);
					
		}
		
		boolean flag=DAOFactory.getContactsDAO().createContact(contact);

		
		if(flag){
			
			request.setAttribute("user", contact.getFirstName()+" "+contact.getLastName());
			page=ConfigurationManager.getInstance().getProperty(ConfigurationManager.MAIN_PAGE_PATH);
		}else{
			
			request.setAttribute("errorMessage", MessageManager.getInstance().getProperty(
					MessageManager.LOGIN_ERROR_MESSAGE));
			
			page=ConfigurationManager.getInstance().getProperty(
					ConfigurationManager.REGISTRATION_PAGE);
		}
		return page;
	}

}
